﻿Imports Business
Public Class frmLoginFingerKeeper

    Public gnCommHandleIndex As Long
    Public strServer As String

    Public Sub New(ByVal strServer As String)

        ' This call is required by the designer.
        InitializeComponent()
        strServer = strServer
        ' Add any initialization after the InitializeComponent() call.
        'Load cboDevice Mode
        cboMode.Items.Add("Network")
        cboMode.Items.Add("USB")
        cboMode.Items.Add("Serial")
        cboMode.SelectedIndex = 0
        'Load default value
        txtMachineNo.Text = "1"
        txtLisence.Text = "1261"
        'default value Network
        txtIPAddress.Text = "192.168.001.123"
        txtPort.Text = "5005"
        txtPassword.Text = "2"
        txtTimeDelay.Text = "5000"
        'default value Serial
        cboComPort.Items.Add("1")
        cboComPort.Items.Add("2")
        cboComPort.Items.Add("3")
        cboComPort.Items.Add("4")
        cboComPort.Items.Add("5")
        cboComPort.SelectedIndex = 0
        cboBaudrate.Items.Add("9600")
        cboBaudrate.Items.Add("19200")
        cboBaudrate.Items.Add("38400")
        cboBaudrate.Items.Add("57600")
        cboBaudrate.Items.Add("115200")
        cboBaudrate.SelectedIndex = 0

    End Sub

    Private Sub cboMode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMode.SelectedIndexChanged
        If cboMode.SelectedIndex = 0 Then
            grpNetwork.Visible = True
            grpSerial.Visible = False
        End If
        If cboMode.SelectedIndex = 1 Then
            grpNetwork.Visible = False
            grpSerial.Visible = False
        End If
        If cboMode.SelectedIndex = 2 Then
            grpSerial.Visible = True
            grpNetwork.Visible = False
        End If
    End Sub

    Private Sub chkUsingModem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUsingModem.CheckedChanged
        If chkUsingModem.Checked = False Then
            txtTelNo.Enabled = False
            txtTimeDialing.Enabled = False
        Else
            txtTelNo.Enabled = True
            txtTimeDialing.Enabled = True
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        lblMessage.Text = "Working..."
        Application.DoEvents()
        Dim bsFKAttend As New BSFKAttend(strServer)
        'Get gnCommHandleIndex value
        Try
            If cboMode.SelectedIndex = 0 Then
                gnCommHandleIndex = bsFKAttend.CheckConnectNet(CInt(txtMachineNo.Text), txtIPAddress.Text, CInt(txtPort.Text), CInt(txtTimeDelay.Text),
                                           chkUDP.Checked, CInt(txtPassword.Text), CInt(txtLisence.Text))
            Else
                If cboMode.SelectedIndex = 1 Then
                    'MsgBox(bsFKAttend.CheckConnectUSB(CInt(txtMachineNo.Text), CInt(txtLisence.Text)))
                    gnCommHandleIndex = bsFKAttend.CheckConnectUSB(CInt(txtMachineNo.Text), CInt(txtLisence.Text))
                Else
                    gnCommHandleIndex = bsFKAttend.CheckConnectSerial(CInt(txtMachineNo.Text), CInt(cboComPort.SelectedItem), CInt(cboBaudrate.SelectedItem),
                                             chkUsingModem.Checked, txtTelNo.Text, CInt(txtTimeDialing.Text), CInt(txtLisence.Text))
                End If
            End If
            lblMessage.Text = ""
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Application.DoEvents()
        'Open Form Manager FP's Data
        If gnCommHandleIndex > 0 Then
            Me.Close()
            Dim frmFingerKeeper As New frmFingerKeeper(gnCommHandleIndex, strServer)
            frmFingerKeeper.ShowDialog()
            'MsgBox("OK")
        Else
            MsgBox("No comn")
        End If
    End Sub

    Private Sub txtMachineNo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMachineNo.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 4
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtLisence_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLisence.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 6
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtTelNo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTelNo.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 8
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtTimeDialing_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTimeDialing.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 4
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtPort_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPort.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 4
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtPassword_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 8
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtTimeDelay_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTimeDelay.KeyPress
        Dim txtCell As TextBox = sender
        txtCell.MaxLength = 4
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
End Class